from rest_framework.views import APIView
from django.http import JsonResponse
from rest_framework import status
from .models import Tag, TagSerializer
from django.db.models import Q


class TagCreateView(APIView):
    def post(self, request, format=None):
        serializer = TagSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse({"code": "200", "data": serializer.data, "message": "创建成功", "ok": "true"},
                                status=status.HTTP_200_OK)
        return JsonResponse({"code": "400", "data": serializer.errors, "message": "创建失败", "ok": "false"},
                            status=status.HTTP_400_BAD_REQUEST)


class TagListView(APIView):
    def get(self, request, format=None):
        tags = Tag.objects.all()
        serializer = TagSerializer(tags, many=True)
        return JsonResponse({"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
                            status=status.HTTP_200_OK)


class BlogTagListView(APIView):
    def get(self, request, format=None):
        tags = Tag.objects.all()
        serializer = TagSerializer(tags, many=True)
        return JsonResponse({"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
                            status=status.HTTP_200_OK)


class TagDetailView(APIView):
    def get_object(self, pk):
        try:
            return Tag.objects.get(pk=pk)
        except Tag.DoesNotExist:
            return None

    def get(self, request, format=None):
        pk = request.data.get('id')
        if not pk:
            return JsonResponse({"code": "400", "data": None, "message": "缺少标签ID", "ok": "false"},
                                status=status.HTTP_400_BAD_REQUEST)
        tag = self.get_object(pk)
        if tag is None:
            return JsonResponse({"code": "404", "data": None, "message": "标签不存在", "ok": "false"},
                                status=status.HTTP_404_NOT_FOUND)
        serializer = TagSerializer(tag)
        return JsonResponse({"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
                            status=status.HTTP_200_OK)

    def put(self, request, format=None):
        pk = request.data.get('id')
        if not pk:
            return JsonResponse({"code": "400", "data": None, "message": "缺少标签ID", "ok": "false"},
                                status=status.HTTP_400_BAD_REQUEST)
        tag = self.get_object(pk)
        if tag is None:
            return JsonResponse({"code": "404", "data": None, "message": "标签不存在", "ok": "false"},
                                status=status.HTTP_404_NOT_FOUND)
        serializer = TagSerializer(tag, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse({"code": "200", "data": serializer.data, "message": "更新成功", "ok": "true"},
                                status=status.HTTP_200_OK)
        return JsonResponse({"code": "400", "data": serializer.errors, "message": "更新失败", "ok": "false"},
                            status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        tag = self.get_object(pk)
        if tag is None:
            return JsonResponse({"code": "404", "data": None, "message": "标签不存在", "ok": "false"},
                                status=status.HTTP_404_NOT_FOUND)
        tag.delete()
        return JsonResponse({"code": "200", "data": None, "message": "删除成功", "ok": "true"},
                            status=status.HTTP_200_OK)


class TagSimilarView(APIView):
    def get(self, request, format=None):
        keyword = request.query_params.get('keyword', '')
        if not keyword:
            return JsonResponse({"code": "400", "data": None, "message": "缺少关键词", "ok": "false"},
                                status=status.HTTP_400_BAD_REQUEST)

        # 使用 Q 对象进行模糊查询
        tags = Tag.objects.filter(Q(name__icontains=keyword))
        serializer = TagSerializer(tags, many=True)
        return JsonResponse({"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
                            status=status.HTTP_200_OK)


class BlogTagSimilarView(APIView):
    def get(self, request, format=None):
        keyword = request.query_params.get('keyword', '')
        if not keyword:
            return JsonResponse({"code": "400", "data": None, "message": "缺少关键词", "ok": "false"},
                                status=status.HTTP_400_BAD_REQUEST)

        # 使用 Q 对象进行模糊查询
        tags = Tag.objects.filter(Q(name__icontains=keyword))
        serializer = TagSerializer(tags, many=True)
        return JsonResponse({"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
                            status=status.HTTP_200_OK)